package com.ruoyi.canteen.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;

import com.ruoyi.canteen.domain.dto.MealRecordDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.canteen.mapper.OrderRecordMapper;
import com.ruoyi.canteen.domain.OrderRecord;
import com.ruoyi.canteen.service.IOrderRecordService;

/**
 * 报餐记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class OrderRecordServiceImpl implements IOrderRecordService {
    @Autowired
    private OrderRecordMapper orderRecordMapper;

    /**
     * 查询报餐记录
     *
     * @param id 报餐记录主键
     * @return 报餐记录
     */
    @Override
    public OrderRecord selectOrderRecordById(Long id) {
        return orderRecordMapper.selectOrderRecordById(id);
    }

    /**
     * 查询报餐记录列表
     *
     * @param orderRecord 报餐记录
     * @return 报餐记录
     */
    @Override
    public List<OrderRecord> selectOrderRecordList(OrderRecord orderRecord) {
        return orderRecordMapper.selectOrderRecordList(orderRecord);
    }

    /**
     * 新增报餐记录
     *
     * @param orderRecord 报餐记录
     * @return 结果
     */
    @Override
    public int insertOrderRecord(OrderRecord orderRecord) {
        orderRecord.setCreateTime(DateUtils.getNowDate());
        return orderRecordMapper.insertOrderRecord(orderRecord);
    }

    /**
     * 修改报餐记录
     *
     * @param orderRecord 报餐记录
     * @return 结果
     */
    @Override
    public int updateOrderRecord(OrderRecord orderRecord) {
        orderRecord.setUpdateTime(DateUtils.getNowDate());
        return orderRecordMapper.updateOrderRecord(orderRecord);
    }

    /**
     * 批量删除报餐记录
     *
     * @param ids 需要删除的报餐记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderRecordByIds(Long[] ids) {
        return orderRecordMapper.deleteOrderRecordByIds(ids);
    }

    /**
     * 删除报餐记录信息
     *
     * @param id 报餐记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderRecordById(Long id) {
        return orderRecordMapper.deleteOrderRecordById(id);
    }

    @Override
    public List<OrderRecord> selectRecordByUMId(Long userId, Long selectedDate) {
        return orderRecordMapper.selectRecordByUMId(userId, selectedDate);
    }

    @Override
    public OrderRecord selectOrderRecordByDto(Long userId, String mealType, Long weekNumber, Long weeklySetMealId) {
        return orderRecordMapper.selectOrderRecordByDto(userId, mealType, weekNumber, weeklySetMealId);
    }

    @Override
    public OrderRecord selectOrderRecordByCarNo(String cardNo, String mealType, String dateStr, String weekStr) {

        return orderRecordMapper.selectOrderRecordByCarNo(cardNo, mealType, dateStr, weekStr);
    }

    @Override
    public List<MealRecordDTO> queryChartByDate(String startDate, String endDate, String weekday) {
        // 转换日期为周次格式
        int startWeekNumber = dateToWeekNumber(startDate);
        int endWeekNumber = dateToWeekNumber(endDate);
        return orderRecordMapper.queryChartByDate(startWeekNumber, endWeekNumber, weekday);
    }
    /**
     * 将日期字符串转换为整型格式 yyyymmdd
     *
     * @param dateStr 日期字符串，格式为 yyyy-MM-dd
     * @return 整型格式的日期 yyyymmdd
     */
    private int dateToWeekNumber(String dateStr) {
        try {
            // 定义日期格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 解析日期字符串
            Date date = sdf.parse(dateStr);

            // 将日期重新格式化为 yyyymmdd 的整型
            sdf.applyPattern("yyyyMMdd");
            String formattedDate = sdf.format(date);

            // 转换为整型并返回
            return Integer.parseInt(formattedDate);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format: " + dateStr + ". Expected format is yyyy-MM-dd.");
        }
    }
}
